package bkq.viettravel.bll;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.webkit.WebChromeClient.CustomViewCallback;
import bkq.viettravel.database.SqliteServices;
import bkq.viettravel.dto.FavoriteDTO;
import bkq.viettravel.interf.IObjectCommon;

public class FavoriteBLL implements IObjectCommon<FavoriteDTO>{
	
	SqliteServices service = null;
	
	public FavoriteBLL(Context context) {
		service = new SqliteServices(context);
	}

	@Override
	public ArrayList<FavoriteDTO> getAll(String sql) {
		ArrayList<FavoriteDTO> tmp = new ArrayList<FavoriteDTO>();
		FavoriteDTO favorite = null;
		Cursor c = service.queryData(sql);
		try {
			if(c.moveToFirst())
			{
				do
				{
					favorite = new FavoriteDTO();
					favorite.setId(Integer.parseInt(c.getString(c.getColumnIndex("id"))));
					favorite.setIdTravel(Integer.parseInt(c.getString(c.getColumnIndex("idTravel"))));
					tmp.add(favorite);
				}
				while(c.moveToNext());
			}
			c.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return tmp.size() == 0 ? null : tmp;
		
	}

	@Override
	public ArrayList<FavoriteDTO> getAll() {
		return this.getAll("select * from Favorites");
	}

	@Override
	public boolean insert(FavoriteDTO obj) {
		String sql = "insert into Favorites (idTravel) values ("+obj.getIdTravel()+")";
		boolean check = false;
		try {
			if(!isFavorite(obj.getIdTravel()))
				service.exeSQL(sql);
			check = true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return check;
	}
	/*
	 * kiem tra xem mot Travel co id la idTravel da nam trong favorites chua
	 * tra ve true neu da la favorite, size != 0;
	 * tra ve false neu chua nam trong favorite , size == 0
	*/
	public boolean isFavorite(int idTravel)
	{
		boolean check = false ;
		String sql = "select * from Favorites where idTravel = "+idTravel+"";
		ArrayList<FavoriteDTO> tmp = this.getAll(sql);
		if(tmp.size()== 0)
			check = false;
		else 
			check = true;
		return check;
	}

}
